package pt.mleiria.crypto;

import static java.lang.Long.toHexString;

import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;

import org.apache.log4j.Logger;

import pt.mleiria.messages.MessageVO;

public class Main {
	private static final Logger LOGGER = Logger.getLogger("Main");

	public static void main(String[] args) throws Exception{
		String plainText1 = "teste123";
		byte[] plainText = plainText1.getBytes("UTF-8");
		// generate an RSA key
		System.out.println("\nStart generating RSA key");
		KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
		keyGen.initialize(1024);
		KeyPair key = keyGen.generateKeyPair();
		System.out.println("Finish generating RSA key");
		//
		// get an RSA cipher object and print the provider
		Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
		System.out.println("\n" + cipher.getProvider().getInfo());
		//
		// encrypt the plaintext using the public key
		System.out.println("\nStart encryption");
		cipher.init(Cipher.ENCRYPT_MODE, key.getPublic());
		byte[] cipherText = cipher.doFinal(plainText);
		System.out.println("Finish encryption: ");
		System.out.println(new String(cipherText, "UTF8"));
		//
		// decrypt the ciphertext using the private key
		System.out.println("\nStart decryption");
		cipher.init(Cipher.DECRYPT_MODE, key.getPrivate());
		byte[] newPlainText = cipher.doFinal(cipherText);
		System.out.println("Finish decryption: ");
		System.out.println(new String(newPlainText, "UTF8"));
	}

	public static void main4(String[] args) throws Exception {

		String plainText1 = "teste123";
		byte[] plainText = plainText1.getBytes("UTF-8");
		//
		// get a DES private key
		System.out.println("\nStart generating DES key");
		KeyGenerator keyGen = KeyGenerator.getInstance("DES");
		keyGen.init(56);
		Key key = keyGen.generateKey();
		System.out.println("Finish generating DES key");
		//
		// get a DES cipher object and print the provider
		Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
		System.out.println("\n" + cipher.getProvider().getInfo());
		//
		// encrypt using the key and the plaintext
		System.out.println("\nStart encryption");
		cipher.init(Cipher.ENCRYPT_MODE, key);
		byte[] cipherText = cipher.doFinal(plainText);
		System.out.println("Finish encryption: ");
		System.out.println(new String(cipherText, "UTF8"));
		//
		// decrypt the ciphertext using the same key
		System.out.println("\nStart decryption");
		cipher.init(Cipher.DECRYPT_MODE, key);
		byte[] newPlainText = cipher.doFinal(cipherText);
		System.out.println("Finish decryption: ");
		System.out.println(new String(newPlainText, "UTF8"));
	}

	public static void main3(String[] args) throws Exception {

		String plainText1 = "teste123";
		byte[] plainText = plainText1.getBytes("UTF-8");
		//
		// get a key for the HmacMD5 algorithm
		System.out.println("\nStart generating key");
		KeyGenerator keyGen = KeyGenerator.getInstance("HmacMD5");
		SecretKey MD5key = keyGen.generateKey();
		System.out.println("Finish generating key");
		//
		// get a MAC object and update it with the plaintext
		Mac mac = Mac.getInstance("HmacMD5");
		mac.init(MD5key);
		mac.update(plainText);
		//
		// print out the provider used and the MAC
		System.out.println("\n" + mac.getProvider().getInfo());
		System.out.println("\nMAC: ");
		System.out.println(new String(mac.doFinal(), "UTF8"));
	}

	public static void main2(String[] args) throws Exception {
		LOGGER.info(CryptoUtils.cipherAndEncodeBase64SHA1("teste123"));
	}

	/**
	 * @param args
	 */
	public static void main1(String[] args) {
		try {
			LOGGER.info(0x4FF6026E);
			LOGGER.info(0xFF000001);
			// String accountBlockStr = "9912598765432100";
			String accountBlockStr = "9912123456789012";

			char[] accountBlockChar = accountBlockStr.toCharArray();

			// byte[] encodedBlock = new byte[accountBlockChar.length];
			byte[] encodedBlock = accountBlockStr.getBytes("UTF-8");

			String str = "";
			for (int i = 0; i < encodedBlock.length; i++) {
				// encodedBlock[i] = (byte)accountBlockChar[i];
				str += toHexString(encodedBlock[i]);
				str += ":";
			}
			LOGGER.info("HEXADE:" + str);
			LOGGER.info("LENGTH=" + encodedBlock.length);

			LOGGER.info(0xFF00);
			// LOGGER.info("HEXADE:" + toHexString(b64Encoded[0]));
			// HEXADE:39:39:31:32:31:32:33:34:35:36:37:38:39:30:31:32:
			/*
			 * LOGGER.info(0x39); int i = 0x39; byte b = (byte)i;
			 * LOGGER.info("BINARY:" + toBinaryString(b)); LOGGER.info("HEXADE:"
			 * + toHexString(b)); LOGGER.info(b);
			 */
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private static void test2() {
		String input = "1393841120120508155840697823.00701846";
		try {
			LOGGER.info(CryptoUtils.cipherSHA1(input));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static void test1() {
		Nshift caeser = new Nshift();
		caeser.setKey("3");
		// caeser.setCipherText("PBFBPFJ");
		MessageVO msg = new MessageVO();
		msg.setMessage("PBFBPFJ");
		caeser.encryptText();
		System.out.println(caeser.getMessageVO().getMessageDigest());
		// caeser.dencryptText();
		// System.out.println(caeser.getPlainText());
	}

}
